<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/3/20
 * Time: 上午11:55
 *
 * 功能：自动取消可以取消的订单
 * 版本：v1.0
 *
 *
 * 规则：1分钟拉取一次待处理数据
 *
 * 注意：可以手动，如果手动处理，则可以修复指定一个分钟内的未正确取消的订单
 *      例如 php cancelOrder.php 2018-03-20 11:19:00  修复指定时间的未取消的订单
 *
 *
 */


ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\model\trade\OrderPackage as mOrderPackage;
use Biz\model\trade\ShopCourier as mShopCourier;
use Biz\service\Event;

/*
 * 按照以下规则获取订单列表
 *
 *
 * 已部分发货，但是把全部商品都发完时，没有正确修改订单状态
 *
 */

if (isset($argv[1])) {
    $stopMiunte = $argv[1];
} else {
    $stopMiunte = date('Y-m-d H:i:00', time());
}

$startMiunte = date('Y-m-d H:i:00', strtotime($stopMiunte) - 60);


$where = [
    'delivery_time[<>]' => [$startMiunte, $stopMiunte],
    'ORDER' => ['id' => 'DESC']
];

try {
    //取出待处理的数据
    $orderPackageList = mOrderPackage::select("*", $where);

    if (empty($orderPackageList)) {
        throw new \Exception("当前时刻没有待处理的订单数据");
    }

    foreach ($orderPackageList as $orderPackage) {

        try {

            $orderNo = $orderPackage['order_no'];

            //获取订单中所有的skuid列表
            $skuIdList = mOrderGoods::getSkuIds($orderNo);

            if (empty($skuIdList)) {
                continue;
            }

            $checkSendAll = mOrderPackage::checkSendAll($orderNo, [], $skuIdList);
            //查询订单信息
            $orderInfo = mOrder::getOrderInfo($orderNo);

            if ($checkSendAll && mOrder::checkStatusCanShip($orderInfo['status'])) {
                $data = [];
                $data['status'] = mOrder::STATUS_SEND_OUT;
                $data['delivery_time'] = $orderPackage['delivery_time'];
                $data['autoconfirm_time'] = date('Y-m-d H:i:s', strtotime($orderPackage['delivery_time']) + mOrder::MAX_WAIT_AUTO_CONFIRM_TIME);


                mOrder::updateData($data, ['order_no' => $orderNo, 'status' => mOrder::STATUS_SEND_PART_OUT]);


                $lastPackageInfo = mOrderPackage::getLastPackageInfo($orderNo);

                $courierInfo = mShopCourier::getCourier(['courier_id'], ['courier_name' => $lastPackageInfo['express_company']]);


                Event::push(Event::ORDER_SHIP_ALL_DELIVERY_SUCCESS, [
                    'shop_id' => $orderInfo['shop_id'],
                    'order_no' => $orderNo,
                    'sku_ids' => array_values($skuIdList),
                    'refunding_status' => $orderInfo['refunding_status'],
                    'courier_id' => $courierInfo['courier_id'],
                    'express_company' => $lastPackageInfo['express_company'],
                    'express_no' => $lastPackageInfo['express_no'],
                    'is_multi' => 0,
                    'package_id' => $lastPackageInfo['id'],
                    'is_package_delivery' => 1,
                    'is_delivery_all' => 1,
                    'uniq_idx' => ['order_no', 'event_tag']
                ]);
            }


        } catch (\Exception $e) {

            echo sprintf("%s    时刻:%s    order_no:%s    异常：%s  \n", date('Y-m-d H:i:s'), $stopMiunte, $orderNo, json_encode([
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine(),
                'e_msg' => $e->getMessage()
            ], JSON_UNESCAPED_UNICODE));

        } finally {
            echo sprintf("%s    时刻:%s   sql日志：%s   \n", date('Y-m-d H:i:s'), $stopMiunte, mOrder::sqlLogs());

            mOrder::clearLogs();
        }
    }
} catch (\Exception $e) {
    echo sprintf("%s    时刻:%s    异常：%s  \n", date('Y-m-d H:i:s'), $stopMiunte, json_encode([
        'e_file' => $e->getFile(),
        'e_line' => $e->getLine(),
        'e_msg' => $e->getMessage()
    ], JSON_UNESCAPED_UNICODE));

}





